#ifndef LAYERCHECKBOXDELEGATE_H
#define LAYERCHECKBOXDELEGATE_H

#endif // LAYERCHECKBOXDELEGATE_H
#include <QCheckBox>
#include <QPainter>
#include <QStyledItemDelegate>
#include <QStandardItemModel>
#include <qgslayertreeview.h>
#include <qgslayertreemodel.h>
#include <qgsproject.h>
#include <QSplitter>
#include <QVBoxLayout>
#include <QWidget>
#include <QVariant>
#include <qgsapplication.h>
#include <qgslayertreenode.h>

class LayerCheckBoxDelegate : public QStyledItemDelegate {
    Q_OBJECT

public:
    LayerCheckBoxDelegate(QObject* parent = nullptr) : QStyledItemDelegate(parent) {}

    // 绘制每个项时插入复选框
    void paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const override {
        QStyledItemDelegate::paint(painter, option, index);

        if (index.isValid()) {
            // 检查是否是叶子节点，决定是否显示复选框
            bool isChecked = index.data(Qt::CheckStateRole).toBool();
            QRect checkBoxRect = option.rect.adjusted(4, 4, -4, -4); // 设置复选框的大小和位置

            // 绘制复选框
            QStyleOptionButton checkBoxStyle;
            checkBoxStyle.state = isChecked ? QStyle::State_On : QStyle::State_Off;
            checkBoxStyle.rect = checkBoxRect;

            QApplication::style()->drawControl(QStyle::CE_CheckBox, &checkBoxStyle, painter);
        }
    }

    // 创建复选框的编辑器
    QWidget *createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const override {
        QCheckBox *checkBox = new QCheckBox(parent);
        checkBox->setChecked(index.data(Qt::CheckStateRole).toBool());
        return checkBox;
    }

    // 设置复选框状态时更新数据
    void setEditorData(QWidget *editor, const QModelIndex &index) const override {
        QCheckBox *checkBox = static_cast<QCheckBox*>(editor);
        checkBox->setChecked(index.data(Qt::CheckStateRole).toBool());
    }

    // 更新数据模型
    void setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const override {
        QCheckBox *checkBox = static_cast<QCheckBox*>(editor);
        model->setData(index, checkBox->isChecked(), Qt::CheckStateRole);
    }
    // 处理复选框的点击事件
    bool editorEvent(QEvent *event, QAbstractItemModel *model, const QStyleOptionViewItem &option,
                     const QModelIndex &index) override
    {
        qDebug()<<"AAAAAAAAAAAAAAAAAAAAA";
        // 只有在点击事件时才处理复选框的状态
        if (event->type() == QEvent::MouseButtonRelease)
        {
            // 切换复选框状态
            bool currentState = index.data(Qt::CheckStateRole).toBool();
            bool newState = !currentState;
qDebug()<<"MouseButtonRelease currentState="<<currentState<<"newState="<<newState;
            qDebug()<<"MouseButtonRelease index="<<index;
qDebug()<<"MouseButtonRelease index.column="<<index.column();
            qDebug() << "index.internalPointer=" << index.internalPointer();
QgsLayerTreeNode* node = static_cast<QgsLayerTreeNode*>(index.internalPointer());
            qDebug() << "node=" << node;
/*
                // 更新模型中的数据
     //bool ret=mysetData(index, newState, Qt::CheckStateRole);
      //qDebug()<<"MouseButtonRelease mysetdata="<<ret;
//model->setData(index, newState);
qDebug()<<"model->setData()="<<model->setData(index, newState,Qt::CheckStateRole);
            //if (model->setData(index, newState, Qt::CheckStateRole) == true)
            {
                // 通知视图数据已改变，确保视图更新
                qDebug()<<"bbbbbbbbbbbbbbAAA";
                model->dataChanged(index, index,{Qt::CheckStateRole});

                // 返回 true，表示事件已经处理
                return true;
            }
        }
*/

Qt::CheckState checkState = newState ? Qt::Checked : Qt::Unchecked;
qDebug() << "model type: " << model->metaObject()->className();
/*
QStandardItemModel *stdModel = dynamic_cast<QStandardItemModel *>(model);
qDebug()<<"stdModel="<<stdModel;
if (stdModel) {
    qDebug()<<"checkState="<<checkState;
    QStandardItem *item = stdModel->itemFromIndex(index);
    qDebug()<<"item="<<item;
    if (item) {
        item->setCheckState(checkState); // 例如更新状态
    }
}
*/
//QgsLayerTreeProxyModel *layerModel = dynamic_cast<QgsLayerTreeProxyModel *>(model);
//qDebug() << "layerModel=" << layerModel;
//if (layerModel) {
    qDebug() << "checkState=" << checkState;
    // 使用itemFromIndex获取项
    //QgsLayerTreeNode *node = layerModel->itemFromIndex(index);


    // 使用数据获取方法
    //QVariant itemData = layerModel->data(index, Qt::DisplayRole);
    // 假设返回的数据是QgsLayerTreeNode类型
    //QgsLayerTreeNode *node = qvariant_cast<QgsLayerTreeNode *>(itemData);

    //QgsLayerTreeNode *node = static_cast<QgsLayerTreeNode *>(index.internalPointer());

    if (node) {
        // 假设你是想更改节点的选中状态，可以通过某些方法来更改
        // 例如，假设节点有设置选中状态的方法
        node->setItemVisibilityChecked(checkState);// setCheckState(checkState); // 或者使用适合的方式更新选中状态
    }
}
        return QStyledItemDelegate::editorEvent(event, model, option, index);
    }

    /*
    bool mysetData(const QModelIndex &index, const QVariant &value, int role) {
        if (!index.isValid())
            return false;

        if (role == Qt::CheckStateRole && index.column() == 0) {
            bool checked = value.toBool();
            // 更新模型中的数据
            m_data[index.row()].setChecked(checked);
            emit dataChanged(index, index);  // 通知视图数据已更改
            return true;  // 表示数据更新成功
        }

        return false;  // 角色未处理，返回false
    }
*/
};


